#include "typedef.h"
#include "can_driver.h"


static void CAN1_SetFilterBank(void)
{
	CAN_filter_bank_init16bit(0,CAN_FIFO_0,CAN_FilterMode_Mask, CAN_TYPE_STD,0x200,0x7E0,0x300,0x7E0);
	//CAN_filter_bank_init16bit(1,CAN_FIFO_0,CAN_FilterMode_List, CAN_TYPE_STD,0x129,0x12A,0x12B,0x12C);
	CAN_filter_bank_init16bit(2,CAN_FIFO_0,CAN_FilterMode_List, CAN_TYPE_STD,0x129,0x12A,0x12B,0x12C);
	CAN_filter_bank_init16bit(3,CAN_FIFO_0,CAN_FilterMode_List, CAN_TYPE_STD,0x129,0x12A,0x12B,0x12C);

}




void can_bsp_init(void (*rx_callback)(const CAN_MSG_T*))
{
	CanDevice_s * Can1Node;

	Can1Node = CAN_apply_device(CAN_1);
	if(Can1Node == NULL)
		return;

	Can1Node->Remap 		= 0;
	Can1Node->u16Baudrate 	= CAN_125K;
	Can1Node->RxProcessFunc	= rx_callback;

	CAN_init_device(CAN_1);
	CAN1_SetFilterBank();
}
u8 can_bsp_standard_send(u16 id,const u8 * data,u8 len)
{
	CAN_MSG_T msg;
	msg.StdId = id;
	msg.IDE = CAN_TYPE_STD;
	msg.RTR = CAN_RTR_TYPE_DATA;
	msg.DLC = len<8 ? len : 8;
	for(int i=0; i<msg.DLC; i++)
		msg.Data[i] = data[i];

	return CAN_data_send(CAN_1, &msg);
}

u8 can_bsp_extended_send(u32 id,const u8 * data,u8 len)
{
	CAN_MSG_T msg;
	msg.ExtId = id;
	msg.IDE = CAN_TYPE_EXT;
	msg.RTR = CAN_RTR_TYPE_DATA;
	msg.DLC = len<8 ? len : 8;
	for(int i=0; i<msg.DLC; i++)
		msg.Data[i] = data[i];

	return CAN_data_send(CAN_1, &msg);

}


